var loading = false;
var num = 90;
var ismobile = /(iPhone|iPad|iPod|iOS|Android)/i.test(navigator.userAgent);
var orderId = '';

function submit() {
	if (loading) {
		return showTip('正在请求，请稍候...');
	}
	var form = document.getElementById('form');
	var params = {
		goodsname: form['price'].options[form['price'].selectedIndex].text,
		price: form['price'].value,
		username: form['username'].value,
		phone: form['phone'].value,
		email: form['email'].value,
		remark: form['remark'].value,
		type: form['type'].value,
	};
	try {
		if (!params.username) throw new Error('请输入您的姓名');
		if (!/^1[345789]\d{9}$/.test(params.phone || '')) {
			throw new Error('请正确填写手机号码');
		}
		if (!/^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/.test(params.email)) {
			throw new Error('请正确填写电子邮箱');
		}
	} catch (err) {
		showTip(err.message);
		return false;
	}
	loading = true;
	Request.post(
		'/api/pay/order',
		params,
		function(data) {
			loading = false;
			if (ismobile && data.result.pay.ZK_TYPE === 1 && data.result.pay.ZK_ISCOMMON === '1') {
				window.open(data.result.pay.ZK_DECODE);
			} else {
				showQrCode(data.result.form, data.result.pay, data.message);
			}
		},
		function(err) {
			loading = false;
			showTip(err.message);
		}
	);
}

function showTip(msg, background) {
	document.querySelector('.error .body').textContent = msg;
	document.querySelector('.error .header').style.background = background || '#ff2727';
	document.querySelector('.error').style.display = 'flex';
}

function closeTip() {
	document.querySelector('.error').style.display = 'none';
}

function showQrCode(form, pay, url) {
	orderId = form.ZK_ID;
	document.querySelector('.pay .owner').textContent = pay.ZK_SELLER;
	document.querySelector('.pay .price').textContent = '￥ ' + form.ZK_PRICE;
	document.querySelector('.pay .qrcode').src = url;
	document.querySelector('.pay .scan').style.display = 'none';
	if (form.ZK_TYPE === 1) {
		if (ismobile) {
			document.querySelector('.pay .scan.alipay').innerHTML = '长按识别或保存到相册<br />使用支付宝继续付款';
		}
		document.querySelector('.pay .scan.alipay').style.display = 'block';
	} else if (form.ZK_TYPE === 2) {
		if (ismobile) {
			document.querySelector('.pay .scan.weixin').innerHTML = '长按识别或保存到相册<br />使用微信继续付款';
		}
		document.querySelector('.pay .scan.weixin').style.display = 'block';
	}
	document.querySelector('.pay').style.display = 'flex';
	var checkTimer = function() {
		document.querySelector('.pay .second').innerHTML = '请在<label>' + num-- + '</label>秒内完成支付';
		if (num < 0) {
			document.querySelector('.pay .btn').style.display = 'inline-block';
			document.querySelector('.pay .second').textContent = ' ';
			return false;
		}
		var params = '?id=' + form.ZK_ID + '&random=' + new Date().getTime();
		Request.get('/api/pay/check' + params, {}, function(data) {
			if (data.result) {
				showResult();
			} else {
				setTimeout(function() {
					checkTimer();
				}, 1000);
			}
		});
	};
	checkTimer();
}

function closeQrCode() {
	document.querySelector('.pay').style.display = 'none';
}

function showHelp() {
	var msg = prompt('请填写无法支付原因或联系客服(微信:Msg-1995)');
	if (!msg) return false;
	Request.post(
		'/api/pay/error',
		{
			id: orderId,
			msg: msg,
		},
		function(data) {
			if (data.flag === 'True') {
				closeQrCode();
				showTip('反馈成功，我们会尽快与你联系', '#1e88e5');
			}
		}
	);
}

function showResult() {
	closeQrCode();
	document.querySelector('.result').style.display = 'flex';
}

function closeResult() {
	document.querySelector('.result').style.display = 'none';
}
